package org.zn.etl.misc;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.ParserContext;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.zn.datax.common.entity.ColumnInfo;
import org.zn.etl.job.entity.DataMapping;
import org.zn.etl.job.entity.EtlJob;

public class DocHtmlBuilder {
	
	private static ExpressionParser  parser= new SpelExpressionParser();
	private static ParserContext parserContext =new TemplateParserContext("${","}");
	
	private static String jobTempStr="	<tr>\r\n"
/*			+ "		<td class=\"c1\">任务名</td><td colspan=\"3\">${#job.name}</td>\r\n"
			+ "	</tr>	\r\n"
			+ "	<tr>\r\n"
			+ "		<td class=\"c1\">处理类型</td><td colspan=\"3\">	${#job.handlerType}</td>\r\n"
			+ "	</tr>\r\n"
			+ "	<tr>\r\n"
			+ "		<td  class=\"c1\"	colspan=\"2\">source</td><td class=\"c1\" colspan=\"2\">target</td>\r\n"
			+ "	</tr>\r\n"
			+ "	<tr>\r\n"
			+ "		<td class=\"c1\">url</td><td>${#job.source.url}</td>	<td class=\"c1\">url</td><td>${#job.target.url}</td>\r\n"
			+ "	</tr>\r\n"
			+ "	<tr>\r\n"
			+ "		<td class=\"c1\">dbType</td>	 <td>${#job.source.dbType}</td>	<td class=\"c1\">dbType</td>	<td>${#job.target.dbType}</td>\r\n"
			+ "	</tr>\r\n"
			+ "	<tr>\r\n"
			+ "		<td class=\"c1\">factorytype</td><td>${#job.source.factoryType}</td>	<td class=\"c1\">factoryType</td>	<td>${#job.target.factoryType}</td>\r\n"
			+ "	</tr>\r\n"
			+ "	<tr>\r\n"   */
			+ "		<td class=\"c1\">table</td>	<td>${#sourceTable.table}</td>	<td class=\"c1\">table</td>			<td>${#targetTable.table}</td>\r\n"
			+ "	</tr>\r\n"
			+ "	<tr>\r\n"
			+ "		<td class=\"c1\">schema</td>		<td>${#sourceTable.schema}</td>	<td class=\"c1\">schema</td>			<td>		${#targetTable.schema}</td>\r\n"
			+ "	</tr>	\r\n"
			+ "	<tr>\r\n"
			+ "		<td class=\"c1\">sourceSql</td><td>	${#sourceSql}</td>	<td class=\"c1\">postSql</td><td>	${#targetSql}</td>\r\n"
			+ "	</tr>	\r\n"
			+ "	<tr><td colspan=\"4\">columnMapping字段映射</td></tr>";
	
	
		public static String  wappedBody(String doc,String title) {
			
			StringBuilder sb=new StringBuilder();
			
			sb.append("<!DOCTYPE html><html><head>\r\n"
					+ "<meta charset=\"UTF-8\">\r\n"
					+ "<title>"+title+"</title>\r\n"
					+"<style type=\"text/css\">\r\n"
					+ "table {\r\n"
					+ "	width:\"100%\";\r\n"
					+ "}\r\n"
					+ "td{\r\n"
					+ "	width: 480px;\r\n"
					+ "}\r\n"
					+ "td.c1 {\r\n"
					+ "width: 80px;\r\n"
					+ "font-weight: bold;\r\n"
					+ "color:#00bb00;"
					+ "  }\r\n"
					+ "</style>\r\n"
					+"</head><body>");
			sb.append(doc);
			
			sb.append("</body></html>");
			return sb.toString();
		} 
		
		

	private static void bulidDataMapping(EtlJob job,StringBuilder sb) {
		List<DataMapping>  dmlist=job.getDataMappings();
			for (DataMapping dataMapping : dmlist) {
				 
				 sb.append(" <tr><th colspan=\"2\" >").append(dataMapping.getSourceTable().getTable()).append("</th><th colspan=\"2\">")
				 .append(dataMapping.getTargetTable().getTable()).append("</th></tr>");

				 sb.append(" <tr><td>列名</td><td>类型</td><td>列名</td><td>类型</td></tr>");
				 
				 List<Pair<ColumnInfo, ColumnInfo>>  pc=dataMapping.getColumnMappingPairs();
				 
				 if(CollectionUtils.isEmpty(pc)) continue;
				 
				 for (Pair<ColumnInfo, ColumnInfo> pairs : pc) {
					 
					 sb.append(" <tr>");
					 
					 if(Objects.nonNull(pairs.getLeft())) {
						 sb.append("<td>")
						 .append(pairs.getLeft().getName())
						 .append("</td><td>")
						 .append(pairs.getLeft().getType())
						 .append("</td>");
						 
					 }else {
						 sb.append("<td></td><td></td>");
					 }
					 
					 if(Objects.nonNull(pairs.getRight())) {
						 sb.append("<td>")
						 	.append(pairs.getRight().getName())
						 	 .append("</td><td>")
							.append(pairs.getRight().getType())
							.append("</td>");
						 
					 }else {
						 sb.append("<td></td><td></td>");
					 }
					 sb.append("</tr>");

				}
				 sb.append("<tr><td colspan=\"4\" >-----------------------------------------------------------------------------------------<td></tr>");
			}
			 
			 
		 }

	  public static	String bulidTables(EtlJob job){
		 
		StringBuilder sb=new StringBuilder();
		sb.append("<table>");
		bulidDataMapping(job, sb);
		sb.append("</table>");
		  
		return sb.toString();
		  
	}
	  
	  
	  public static	String bulidJobListDoc(List<EtlJob> job,String docName) {
		  
			StringBuilder sb=new StringBuilder();
			
			
			job.sort((o1, o2) -> {

				if(CollectionUtils.isNotEmpty(o1.getDataMappings())&&CollectionUtils.isNotEmpty(o2.getDataMappings()) ) {
					
					DataMapping dm1=o1.getDataMappings().get(0);
					DataMapping dm2=o2.getDataMappings().get(0);
					
					return dm1.getTargetTable().getSchema().compareTo(dm2.getTargetTable().getSchema());
				}

				if(CollectionUtils.isEmpty(o1.getDataMappings())&&CollectionUtils.isEmpty(o2.getDataMappings())) {
					return 0;
				}
					
				if(CollectionUtils.isEmpty(o1.getDataMappings())) {
					return -1;
				}
				
				if(CollectionUtils.isEmpty(o2.getDataMappings())) {
					return 1;
				}

				return 0;
				
			}  );
			
		  
		  for (EtlJob etlJob : job) {
			  bulidJobDoc(etlJob,sb);
		}

		return wappedBody(sb.toString(),docName);
 
	  }
	  
	  
	  public static	void bulidJobDoc(EtlJob job,StringBuilder sb) {
		  
		
		Expression exp=parser.parseExpression(jobTempStr, parserContext);  
		EvaluationContext evalContext=new StandardEvaluationContext();
			
		List<DataMapping> dms= job.getDataMappings();
		
		if(CollectionUtils.isEmpty(dms) ) {
			System.out.println(job.getJobName());
		 return ;	
		}
		
		DataMapping dm=dms.get(0);
		  
		evalContext.setVariable("job", job);
		evalContext.setVariable("sourceTable", dm.getSourceTable());
		evalContext.setVariable("targetTable", dm.getTargetTable());
		evalContext.setVariable("sourceSql", dm.getSourceSql());
		evalContext.setVariable("targetSql", dm.getTargetSql());

		
		sb.append("<table>");
		String  jobhtml=exp.getValue(evalContext, String.class);
		 
		sb.append(jobhtml);
		bulidDataMapping(job, sb);
		sb.append("</table>");

	  }
	  
	
}
